package org.djw.order.controller;

import org.djw.basic.util.AjaxResult;
import org.djw.basic.util.LoginContext;
import org.djw.basic.util.PageList;
import org.djw.order.domain.ProductOrder;
import org.djw.order.query.ProductOrderQuery;
import org.djw.order.service.IProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/productOrder")
public class ProductOrderController {

    @Autowired
    private IProductOrderService productOrderService;
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody Map<String,Object> params, HttpServletRequest request){
        try{
            String payData = productOrderService.submit(params, LoginContext.getCurrentLogininfo(request));
            System.out.println(payData);
            return AjaxResult.me().setResultObj(payData);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("下单失败！"+e.getMessage());
        }
    }

    //后台查询接口
    @PostMapping("/admin")
    public PageList<ProductOrder> admin(@RequestBody ProductOrderQuery query, HttpServletRequest request){
        return productOrderService.admin(query, LoginContext.getCurrentLogininfo(request));
    }

    //用户个人中心我的订单
    @PostMapping("/user")
    public PageList<ProductOrder> user(@RequestBody ProductOrderQuery query, HttpServletRequest request){
        return productOrderService.user(query, LoginContext.getCurrentLogininfo(request));
    }

    @GetMapping("/{orderId}")
    public ProductOrder queryById(@PathVariable("orderId") Long id){
        return productOrderService.queryById(id);
    }
}
